﻿@*
    For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
*@
@{
    Layout = null;
}
@inject Microsoft.AspNetCore.Antiforgery.IAntiforgery Xsrf
@functions{
    public string GetAntiXsrfRequestToken()
    {
        return Xsrf.GetAndStoreTokens(Context).RequestToken;
    }
}
<link href="~/scripts/layui-v2.5.6/layui/css/layui.css" rel="stylesheet" />
<body>
    <form class="layui-form" action="">
        @Html.AntiForgeryToken()
        <div class="layui-form-item">
            <label class="layui-form-label">用户名:</label>
            <div class="layui-input-block">
                <input type="text" id="UserName" name="UserName" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">用户密码:</label>
            <div class="layui-input-block">
                <input type="password" id="UserPassWord" name="UserPassWord" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">昵称:</label>
            <div class="layui-input-block">
                <input type="text" id="UserNickName" name="UserNickName" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">手机号:</label>
            <div class="layui-input-block">
                <input type="text" id="UserPhone" name="UserPhone" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="button" class="layui-btn" onclick="add()">添加</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</body>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="~/scripts/layui-v2.5.6/layui/layui.js"></script>
<script>
    function add(){
        var m = {
            UserName: $("#UserName").val(),
            UserPassWord: $("#UserPassWord").val(),
            UserNickName: $("#UserNickName").val(),
            UserPhone: $("#UserPhone").val(),
           __RequestVerificationToken: $('input[name="__RequestVerificationToken"]').val()
        };
        if(m.UserName == ""){
            alert("用户名不能为空");
            return;
        }
        if(m.UserPassWord == ""){
            alert("密码不能为空");
            return;
        }
        if(m.UserNickName == ""){
            alert("昵称不能为空");
            return;
        }
        if(m.UserPhone == ""){
            alert("手机号不能为空");
            return;
        }        
        $.ajax({
            url: "/User/AddUser",
            data: m,
            type: "post",
            dataType: "text",
            success: function(data){
                if(data > 0){
                    console.log(data);
                    alert("添加成功");
                    // 清空表单
                    $("#UserName").val("");
                    $("#UserPassWord").val("");
                    $("#UserNickName").val("");
                    $("#UserPhone").val("");
                }else{
                    alert("添加失败");
                }
            },
            error: function(xhr, status, error) {
                console.log("Error:", error);
                console.log("Status:", status);
                console.log("Response:", xhr.responseText);
                alert("添加失败：" + error);
            }
        })
    }
</script>

